package org.itboys.mobile.util.baidu;


import com.alibaba.fastjson.JSONObject;
import org.itboys.commons.utils.tools.ListUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 作者：jiangxiong
 * 日期：2016年4月21日
 * 联系方式 ：empty90911@126.com
 * 描述：短信工具
 */
public class BaiDuBankUtil {
	public static String httpUrl="http://apis.baidu.com/datatiny/cardinfo_vip/cardinfo_vip?cardnum=";
	public static String apikey="a3e486a36feeda46f25928e449bbf39f";
	
	
	
	//public static void sendMessage(String cardnum ){
		//String httpUrl = "http://apis.baidu.com/datatiny/cardinfo_vip/cardinfo_vip";
		//String httpArg = "cardnum=6222021203016617804";
		//String jsonResult = request(httpUrl, httpArg);
		//System.out.println(jsonResult);

		/**
		 * @param urlAll
		 *            :请求接口
		 * @param httpArg
		 *            :参数
		 * @return 返回结果
		 */
	/**
	 * 通过银行卡编号获取银行卡信息
	 * @param carnum
	 * @return
	 */
	public static String requestBaiduBankInfo(String carnum) {
		    BufferedReader reader = null;
		    String result = null;
		    StringBuffer sbf = new StringBuffer();
		   String httpUrl="http://apis.baidu.com/datatiny/cardinfo_vip/cardinfo_vip?cardnum="+carnum;
			
		//   "?cardnum=" + carnum;

		    try {
		        URL url = new URL(httpUrl);
		        HttpURLConnection connection = (HttpURLConnection) url
		                .openConnection();
		        connection.setRequestMethod("GET");
		        // 填入apikey到HTTP header
		        connection.setRequestProperty("apikey",  apikey);
		        connection.connect();
		        InputStream is = connection.getInputStream();
		        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
		        String strRead = null;
		        while ((strRead = reader.readLine()) != null) {
		            sbf.append(strRead);
		            sbf.append("\r\n");
		        }
		        reader.close();
		        result = sbf.toString();
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
		    
		    System.out.println(result);
		    return result;
		}
    public static Map<String,Object> getBankInfo(String cardNum){
    	Map<String,Object> myResult=new HashMap<String, Object>();
    	myResult.put("bankName", "");
    	myResult.put("bankLogo", "");
    	myResult.put("status", "");
    	myResult.put("message", "");
    	String baiduResult=	requestBaiduBankInfo(cardNum);
		/*String frontName="status";
		System.out.println("aaaaaaaaaaa---"+result.indexOf(frontName));
		String status= result.substring(result.indexOf(frontName),result.indexOf(frontName)+frontName.length());
		System.out.println("-----"+status);*/
		/*JSONObject jasonObject = JSONObject.fromObject(baiduResult);*/
		JSONObject jasonObject = JSONObject.parseObject(baiduResult);
		Map result = (Map)jasonObject;
		Integer status =(Integer)result.get("status");
		myResult.put("status", status);
		if(status==1){
			Map data =(Map)result.get("data");
			String bankName=(String)data.get("bankname");
			myResult.put("bankName", bankName);
			String cardType=(String)data.get("cardtype");
			myResult.put("cardType", cardType);
			List<Map> bankInfoes =(List<Map>)data.get("bankinfo");
			//System.err.println("bankInfoes"+bankInfoes);
			String bankLogo = "";
			if(ListUtil.isNotNull(bankInfoes)){
				//System.err.println("----------------------");
				Map bankinfo = bankInfoes.get(0);
				bankLogo = (String)bankinfo.get("logourl");
				myResult.put("bankLogo", bankLogo);
			}
			myResult.put("message", "正常");
		}else if(status==-1){
			myResult.put("message", "输入有误");
			
		}else if(status==0){
			myResult.put("message", "0表示没有卡号信息 ");
			
		}
		return myResult;
    }
	public static void main(String[] args) {
		Map<String,Object> myResult=getBankInfo("6236681540006667302");
		System.out.println("---A"+myResult.get("status"));
		System.out.println("---B"+myResult.get("bankName"));
		System.out.println("---C"+myResult.get("bankLogo"));
		System.out.println("---D"+myResult.get("message"));
	}
}
